package com.exampl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.exampl.common.R;
import com.exampl.domain.ShoppingCart;
import com.exampl.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    ShoppingService shoppingService;

    @Autowired
    HttpSession session;

    // 获取购物车中的所有商品
    @GetMapping("/list")
    public R<List<ShoppingCart>> listR(){
        // 获取用户id
        Long user_id = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<ShoppingCart>();
        lqw.eq(ShoppingCart::getUserId, user_id);
        List<ShoppingCart> carts = shoppingService.list(lqw);
        return R.success(carts);
    }

    // 添加菜品到购物车
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        // 获取用户id
        Long user_id = (Long) session.getAttribute("user");
        shoppingCart.setUserId(user_id);
        // 判断购物车是否已存在该商品，若存在则改变数量
        String name = shoppingCart.getName();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<ShoppingCart>();
        lqw.eq(ShoppingCart::getName, name);
        ShoppingCart one = shoppingService.getOne(lqw);
        if(one != null){
            // 存在该商品，改变其数量
            one.setNumber(one.getNumber() + 1);
            shoppingService.updateById(one);
            return R.success(one);
        }else {
            // 不存在，增添一条数据
            shoppingService.save(shoppingCart);
            shoppingCart.setNumber(1);
            return R.success(shoppingCart);
        }
    }

    // 清空购物车
    @DeleteMapping("/clean")
    public R<String> delete(){
        // 获取用户id
        Long user_id = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<ShoppingCart>();
        lqw.eq(ShoppingCart::getUserId, user_id);
        shoppingService.remove(lqw);
        return R.success("清空成功！");
    }

    // 删除购物车中的菜品
    @PostMapping("/sub")
    public R<ShoppingCart> deleteOne(@RequestBody ShoppingCart shoppingCart){
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        // 获取用户id
        Long user_id = (Long) session.getAttribute("user");
        // 改变商品数量，若数量为0则删除该数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<ShoppingCart>();
        lqw.eq(ShoppingCart::getUserId, user_id);
        lqw.eq(dishId != null, ShoppingCart::getDishId, dishId);
        lqw.eq(setmealId != null, ShoppingCart::getSetmealId, setmealId);
        ShoppingCart one = shoppingService.getOne(lqw);
        Integer number = one.getNumber();
        if(--number == 0){
            // 当数量为0时删除该商品
            shoppingService.remove(lqw);
            return R.success(new ShoppingCart());
        }else {
            // 不为0时改变其数量
            one.setNumber(one.getNumber() - 1);
            shoppingService.updateById(one);
            return R.success(one);
        }
    }
}
